Okay.
So today you're going to have a look at some maths that we can add into our into our script.
Some people don't like mind maths.
Some people hate it.
But unfortunately there is quite a lot of maths involved with some of the more complex scripts.
So you'll be looking at.
So you do need to know a decent bit of math.
Okay.
Now this isn't going to be a maths lesson, so not everything we're going to look at will be as commonly
used as others.
There'll be a lot of different areas we're looking at in maths.
Okay.
Um, for the very important ones, I will try to explain in the simplest way I can how the maths works.
But for some I won't explain to you how they work, because it'd be a 20 minute maths lesson explaining
all these different things.
Okay.
I don't want this lesson to be end up like that.
So the key points I will explain to you.
But don't worry, especially if you're younger watching this.
If you don't understand the maths, that's completely fine.
Um, it's probably you probably have to be, um, you know, studying it higher even to learn some of
these things.
Okay.
So don't worry if you don't understand all of the maths.
Okay.
Um, okay.
So add a script into the workspace.
Now, we already know how to do basic math.
Basic of maths.
So adding using the plus signs, taking away using dashes, timing using this little sign here and dividing
using the sign there.
But there's much more that very much allows you to do using maths.
Okay.
And these more advanced kind of maths bits that you need that you might need to do sometimes you can
access through the math functions.
Okay.
So these are found by writing math dot and then these are all the different ones that you can pick.
Okay.
Now quite a few I'm not going to go over all of them but just the ones I think are most important.
Okay.
Um now when might you want to use maths in your game?
Um, well, some of the topics we're going to be learning in the Advanced scripting section do require
a little bit of maths, okay.
Namely two of these math functions, which is why I'm teaching you them now.
So you kind of you're introduced to them before you're kind of forced to use them.
Um, other ones might be used for things like calculating distances.
Um, so I'm not sure if you know about trigonometry that's using triangles to find out the length of
a longest side of a triangle.
Um, but that can be used for things like pathfinding, um, where you can find which player is the
closest and, and for the NPC to go towards that player.
So honestly, um, you'll be introduced to more of these things in the next section in the next scripting
section.
But knowing these functions functions were very useful.
Okay.
So the first one we're going to look at is math dot abs.
And each of these works by you say its name.
You write a pair of brackets.
And inside here you put in a number sometimes 2 or 3 numbers.
Now the description here is returns the absolute value of x.
So you can give x any number.
It can be one.
It can be zero.
It can be minus four.
It can be whatever.
Okay.
Um but in this case what the absolute is quite important.
I'm going to, I'm going to explain what an absolute value is.
Okay.
Um, basically the absolute value of a number is the positive version of the number.
So the absolute value of four is four and the absolute value of minus four is also four.
So if you ignore any negatives that's the absolute value of it.
So all it's doing is getting rid of any negatives.
Okay.
Um so this here.
Oh.
Returns.
Oh the absolute value okay.
So absolute values are just when you ignore any negatives.
This is important when you're calculating distances.
Say for example you want to find out my distance between um this point location here and this part.
And you say okay, it's um, minus five stars along the x axis, minus five is something very difficult
to use because if you want to then take away values from it, when you take away negatives, they become
positive.
It all gets a bit confusing.
So maths dot abs does come in handy quite a few times.
Okay for next two are the ones which are probably, um, probably the second most important you're going
to learn today.
And I'll write them both down first, which is math dot deg and math dot grad.
Now um, basically hopefully you already know about degrees.
Okay.
But it's 360 degrees in a circle.
Okay, that's fine, but you may not know.
But there's a second way to measure angles, which is using something called radians.
Now radians I'm not going to explain properly how they work.
Okay.
Um, but basically 360 degrees is equal to two pi radians.
If you've never heard of pi before pi is a number.
Okay.
It goes 3.1452 and it keeps on going forever.
Okay.
Um, but just pi is a number, and, um, 360 degrees is equal to two lots of pi.
Okay.
You don't need to.
You don't need to remember that or anything.
It's just giving you a bit of an understanding about it.
Okay.
So what math dot deg allows you to.
Do is it will convert an angle from degrees into radians.
Um also returns an angle given in radians to degrees.
So degrees will convert radians to degrees and rads will convert degrees to radians.
Okay, now radians will become a bit more important in the next section.
A couple of things we're looking at will involve you having to use radians.
So unless you want to remember what 360 degrees are in radians you probably want to convert them instead.
Okay.
So you can convert this here which converts.
Radians to degrees.
And this one here will convert degrees to radians.
Okay cool.
Um next up we're looking at powers and square roots.
Okay.
So to cube or square the number we want to put it as a power.
So you'd use math dot pow or math dot P.O.W..
And this here you can see returns X to the power of Y.
If you're not sure what powers mean, um, basically two to the power of three is two times two times
two.
Three to the power of two is three times two.
So basically you're timesing the number by itself.
How many times, however large the number is on top of it?
Okay.
If you don't really understand that too much, it's okay.
Um, powers, you do look at sometimes, but not very commonly, so it's okay if you don't understand
it too much.
Okay, so if we wrote two comma three, that'd be returning two to the power of three.
So it's, um, returns us, um, the first number to the power of the second.
Maybe ask your maths teacher what what power is, um, the opposite way of of of, um, squaring or
cubing a number is using square roots.
Okay.
So math dot sqrt.
Okay.
We'll square root your number.
So if I put here 81 the square root of 81 is nine.
Again don't worry if you don't understand that.
Um it's it's okay if you don't understand it.
It's completely fine.
This here returns the root of the number.
Okay.
Um, cool.
To others, which I think even if you don't know much maths, you'll probably understand them.
Is math dot round?
This is the first one.
And this one will basically round up your number to the closest integer.
Integer is just a whole number like one, 2 or 7.
Okay.
So for example in this case we could put 5.6 and it would round it up to six.
Okay.
So rounds your number to the nearest integer.
Math dot random is probably the most useful one.
Okay, and this here will return a random number between the range.
So you give it a range and it will pick a random number between that range.
So we could say for example 1 to 100.
And it will randomly pick a number 1 to 100.
So picks a random number.
Between your range.
Now this is so useful because you can put this inside of a vector three.
You could put it in a for loop and make a for loop run a random amount of times.
So we want to add randomness into your game.
Math dot random is how to do it.
Okay.
Um, cool.
Now the ones above um, kind of are a bit quite important.
Okay.
Powers and square roots.
If you don't know those too much, it's okay.
They're not too difficult to understand if you do want to learn them.
And if you don't really want to learn them, it's okay.
We probably you probably only use them a couple of times when you are scripting, okay.
But the ones below, um, aren't really that useful.
Um, I haven't used them too much.
I have used them a couple times though, which is why I'm just explaining it to you.
So you are aware of them.
But I'm not going to explain the maths for these ones.
Okay?
So if you don't understand these, it's completely fine.
You're not really missing out on much.
If you do understand it, you know how to do it in Roblox studio.
Okay.
Um, the sine, cosine and tan graphs.
Okay.
Um, you can access in Roblox studio by writing down Math.sin to convert the sine of x Math.cos to convert
the cause of x and math dot tan to convert the tan of x.
Okay, this will be given in radians.
So if you're wanting to put in 180, for example, you would have to write down first math dot rad to
convert your degrees 180 into radians.
Okay, you can also use the arc sin, arc cos and arc tan functions by just writing math dot um a sin
a cos and a tan.
Okay, um, if you don't know about maths, you probably have no idea what I just said there.
But again, it's completely fine.
Okay, if throughout the course we do need to use one of these math functions, I will explain the maths
again and I'll try to make sure that even if you don't really know what the maths is, that you can
still write it down.
Okay.
Um, so just want to show you some examples of math dot random used so I that you can write it in a
for loop.
You could say for example um let's write down these for I is equal to I'm sorry I comma loop there is
equal to one comma Math.random 1 to 100 comma one two.
And here I could say, for example, um, part um oh I, you can reference the part game.
That workspace dot part dot transparency is equal to game workspace dot part dot transparency plus 0.01.
And this will give it a random transparency.
Or it will make this run a random amount of times 1 to 100.
Or instead you could just set game the workspace dot part dot transparency to equal Math.random 1 to
100.
And then if you divide that by 100, you would then get any number from 0.12 to 0.0121.
Okay.
You can't say 0 to 1.
It only returns integers.
So it won't return any negative numbers or numbers with decimal places in them.
Okay.
So hopefully to give you some examples on how to use math dot random.
Again most of these that we'll be using, we will be using a bit more in the next section.
Okay.
Um but that's pretty much it okay.
So well done it for getting through this section.
Okay I know this is I labeled it as the basics in scripting, but to be honest you can make the majority
of things using what you know.
Okay.
Um, in the advanced section we will look at services and things like that, but they'll be allowing
you to do very small things, um, in Roblox studio.
So things like adding game passes or scripting NPCs, but there won't be any major coding topic like
functions, for example, or loops that you would implement into every single script.
So, you know, um, you know, your if you got through this and you understand most of it, you are
still a very good scripter.
Um, but take some time off scripting for now.
Um, and, um, when we do get back on to scripting.
We will be having a look at some of the more advanced ones.
So yeah, hope you've enjoyed these.
Um, this these scripting lessons.
And I will see you in the whichever next section of the course you're going to.